package IntentMessage;

import IntentMessage.IntentSubDialActivity;
import IntentMessage.IntentSubInfoActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.MainActivity;
import com.example.myapplication.Person;
import com.example.myapplication.R;
import com.example.myapplication.Style.StyleActivity;
import com.example.myapplication.TabNavActivity;
import java.util.ArrayList;

public class IntentActivity extends AppCompatActivity {
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intent_test_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Intent实验");
    setSupportActionBar(toolbar);

    BindButton1();
    BindButton2();
    BindButton3();
    BindButton4();
    BindButton5();
    BindButton6();
    BindButton7();
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 1, 0, "返回上一个Activity");
    menu.add(0, 2, 1, "跳转到下一个Activity");

    return true;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId())
    {
      case 1:
        BackToLastActivity();
        break;
      case 2:

        GoToNextActivity();
        break;
    }
    return true;
  }

  private void GoToNextActivity() {
    Intent intent = new Intent(this, StyleActivity.class);
    startActivity(intent);
  }
  private void BackToLastActivity() {
    Intent intent = new Intent(this, TabNavActivity.class);
    startActivity(intent);

  }
  private void BindButton1() {
    Button button = (Button) findViewById(R.id.intent1);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpToComponentName();
      }
    });
  }
  private void BindButton2() {
    Button button = (Button) findViewById(R.id.intent2);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpToInterFilter();
      }
    });
  }
  private void BindButton3() {
    Button button = (Button) findViewById(R.id.intent3);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpToDialActivity();
      }
    });
  }
  private void BindButton4() {
    Button button = (Button) findViewById(R.id.array_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpByArray();
      }
    });
  }
  private void BindButton5() {
    Button button = (Button) findViewById(R.id.list_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpByList();
      }
    });
  }
  private void BindButton6() {
    Button button = (Button) findViewById(R.id.object_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpByObject();
      }
    });
  }
  private void BindButton7() {
    Button button = (Button) findViewById(R.id.bitmap_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpByBitMap();
      }
    });
  }

  private void JumpToComponentName() {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
  }
  private void JumpToInterFilter() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    startActivity(intent);
  }
  private void JumpToDialActivity() {
    Intent intent = new Intent(this, IntentSubDialActivity.class);
    startActivity(intent);
  }
  private void JumpByArray() {
    Intent intent = new Intent(this, IntentSubInfoActivity.class);
    Bundle bundle = new Bundle();
    String[][] infoStr=new String[][]{{"小哈","弱智吧吧主"},{"米浴","孙吧吧主"}};
    bundle.putSerializable("InfoArray",infoStr);

    intent.putExtras(bundle);
    startActivity(intent);
  }
  private void JumpByList() {
    Intent intent = new Intent(this, IntentSubInfoActivity.class);
    Bundle bundle = new Bundle();
    ArrayList<Person> personArrayList = new ArrayList<>();
    personArrayList.add(new Person("小哈", "弱智吧吧主"));
    personArrayList.add(new Person("米浴", "孙吧吧主"));
    bundle.putSerializable("InfoList",personArrayList);

    intent.putExtras(bundle);
    startActivity(intent);
  }
  private void JumpByObject() {
    Intent intent = new Intent(this, IntentSubInfoActivity.class);
    Bundle bundle = new Bundle();
    Person person = new Person("神奇哈姆金丝熊", "照顾圣紫仓");
    bundle.putSerializable("singlePerson", person);
    intent.putExtras(bundle);

    startActivity(intent);
  }
  private void JumpByBitMap() {
    Intent intent = new Intent(this, IntentSubInfoActivity.class);
    Bundle bundle = new Bundle();
    Resources res=getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(res, R.mipmap.img2);
    bundle.putParcelable("bitmap", bitmap);
    intent.putExtras(bundle);

    startActivity(intent);
  }
}
